package Tree;

import java.util.*;

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;A.right = C;
        B.right = E;B.left = D;
        E.right = H;
        C.left = F;C.right = G;

        return A;

    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    public static int nodeSize;

    /**
     * 获取树中节点的个数：遍历思路
     */
    void size(TreeNode root) {
        if(root == null){
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        if(root == null){
            return 0;
        }
        return size2(root.left)+size2(root.right)+1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    void getLeafNodeCount1(TreeNode root) {
        if(root == null){
            return;
        }
        if(root.right==null&&root.left == null){
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        //空节点开始返回
        if(root == null){
            return 0;
        }
        //满足条件视为一个叶子结点
        if(root.right==null&&root.left == null){
            return 1;
        }
        //叶子结点个数等于左子树叶子个数加右子树叶子个数
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }

    /*
    获取第K层节点的个数
     */
    //第k层节点的个数 = 左子树k-1层+右子树k-1层 的个数
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null){
            return 0;
        }
        //第一次即根节点 个数一直为一
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     空间复杂度 O(log2(n))  树的高度 左边递归完释放
     */
    int getHeight(TreeNode root) {
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        int leftLength = getHeight(root.left);
        int rightLength = getHeight(root.right);
        return leftLength>rightLength?leftLength+1:rightLength+1;

    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode leftNode =  find(root.left,val);
        if(leftNode!=null){
            return leftNode;
        }
        TreeNode rightNode =  find(root.right,val);
        if(rightNode!=null){
            return rightNode;
        }
        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root) {

    }


    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        return true;
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null&&q!=null){
            return false;
        }
        if(p!=null&&q==null){
            return false;
        }
        if(p==null&&q==null){
            return true;
        }
        if(p.val != q.val){
            return false;
        }
        else {
            return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
        }



    }

    //翻转二叉树
    public  TreeNode invertTree(TreeNode root){
        if(root == null){
            return null;
        }
        if(root.left==null&&root.right == null){
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }



    //时间复杂度为O（N）的解法 判断平衡二叉树
//        public int getHeight(TreeNode root){
//            if(root == null){
//                return 0;
//            }
//            int leftH = getHeight(root.left);
//            int rightH = getHeight(root.right);
//            if(Math.abs(leftH-rightH)>1){
//                return -1;
//            }
//            if(rightH == -1 || leftH == -1){
//                return -1;
//            }
//            return leftH >rightH ? leftH+1:rightH+1;
//        }
//        public boolean isBalanced(TreeNode root) {
//            if(root == null){
//                return true;
//            }
//            return getHeight(root) != -1;
//        }

    public List<Integer> inorderTraversal(TreeNode root) {
        Stack<TreeNode> s = new Stack<>();
        List<Integer> list = new ArrayList<>();
        while(root!=null||s.isEmpty()){
            while(root!=null){
                s.push(root);
                root = root.left;
            }
            TreeNode top = s.pop();
            //list.add(top.val);
            root = top.right;
        }
           return list;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        Stack<TreeNode> s1 = new Stack<>();
        Stack<TreeNode> s2 = new Stack<>();
        s1 = InitStack(root,s1,p);
        s2 = InitStack(root,s2,q);

        int length1 = s1.size();
        int length2 = s2.size();
        if(length1>length2){
            int len = length1 - length2;
            while(len>0){
                s1.pop();
                len--;
            }
        }else{
            int len = length2 - length1;
            while(len>0){
                s2.pop();
                len--;
            }
        }

        while(length1>0){
            if(s1.peek() == s2.peek()){
                return s1.pop();
            }
            s1.pop();
            s2.pop();
        }

        return null;
    }
    private Stack InitStack(TreeNode root,Stack s,TreeNode t){
        List<List<Integer>> list = new ArrayList<>();
        Queue<TreeNode> queue = new ArrayDeque<>();

        if(root == null){
            return s;
        }
        s.push(root);
        if(root.val == t.val){
            return s;
        }
        InitStack(root.left,s,t);
        InitStack(root.right,s,t);

        return s;
    }
    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder = new StringBuilder();

        return stringBuilder.toString();
    }
    public void tree2strChild(TreeNode root,StringBuilder stringBuilder){
        if(root == null){
            return;
        }
        stringBuilder.append(root.val);
        if(root.left!=null){
            stringBuilder.append('(');
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(')');
        }else {
            if(root.right == null){
                return;
            }else {
                stringBuilder.append("()");
                tree2strChild(root.right,stringBuilder);
            }
        }

        if(root.right!=null){
            stringBuilder.append("(");
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(')');
        }
    }
}
